package com.bcbim.api_web.file.controller;

import com.bcbim.authentication_interface.util.AuthUtil;
import com.bcbim.common.response.annotation.Translate;
import com.bcbim.common.response.vo.ReturnMsg;
import com.bcbim.common.response.vo.ReturnPageMsg;
import com.bcbim.common.sys.Constant;
import com.bcbim.common.sys.ParamException;
import com.bcbim.common.util.ParamCheck;
import com.bcbim.model_server_interface.file.dto.FileListDTO;
import com.bcbim.model_server_interface.file.service.FileService;
import com.bcbim.model_server_interface.file.vo.FileInfoVO;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author: ye
 * @Date: 2022/3/25
 * @Version: V1.0
 **/
@RestController
@RequestMapping("/file")
public class FileController {

    @Reference(protocol = "dubbo", group = "file", version = "1.0.0")
    private FileService fileServiceImpl;

    /**
     * @Description: 文件列表
     * @Param: FileListDTO
     * @Return: ReturnPageMsg
     * @Author: ye
     * @Date: 2022/3/25
     */
    @Translate
    @PostMapping("listFile")
    public ReturnPageMsg listFile(FileListDTO query) {
        try {
            ParamCheck.pageNumCheck(query.getPageNum(), query.getPageSize());
        } catch (ParamException e) {
            return ReturnPageMsg.consFail(ParamException.PARAMETER_ERROR);
        }
        query.setUserId(AuthUtil.getUserIdDecrypt());
        int total = fileServiceImpl.countFile(query);
        List<FileInfoVO> list = new ArrayList<>();
        if (total > 0) {
            query.setStart((query.getPageNum() - 1) * query.getPageSize());
             list = fileServiceImpl.listFile(query);
        }
        return ReturnPageMsg.consSuccess(total, list);
    }

    /**
     * @Description: 删除文件
     * @Param:
     * @Return: ReturnMsg
     * @Author: ye
     * @Date: 2022/3/25
     */
    @PostMapping("removeFile")
    public ReturnMsg removeFile(String fileId) {
        try {
            ParamCheck.idCheck(fileId);
        } catch (ParamException e) {
            return ReturnMsg.consFail(ParamException.PARAMETER_ERROR);
        }
        fileServiceImpl.removeFile(fileId);
        return ReturnMsg.consSuccess(Constant.DEL_SUCCESS);
    }

}
